package lib_model.Order;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Orders_bill_details implements ISql {

	public int id;
	public int orders_bill_id_ref;
	public Date date_create;
	public int quantity;
	public int product_id_ref;

	public Orders_bill_details() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, orders_bill_id_ref, date_create, quantity, product_id_ref from dbo.orders_bill_detail";
		ReturnObj obj = new ReturnObj();
		ArrayList<Orders_bill_details> arr = new ArrayList<Orders_bill_details>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Orders_bill_details a = new Orders_bill_details();
				a.id = rs.getInt("id");
				a.orders_bill_id_ref = rs.getInt("orders_bill_id_ref");
				a.date_create = rs.getDate("date_create");
				a.quantity = rs.getInt("quantity");
				a.product_id_ref = rs.getInt("product_id_ref");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.orders_bill_detail (orders_bill_id_ref,   date_create,   quantity,   product_id_ref) values   ( ?,   GETDATE(),   ?,   ?)";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.orders_bill_id_ref);
			preState.setInt(2, this.quantity);
			preState.setInt(3, this.product_id_ref);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.orders_bill_detail set orders_bill_id_ref = ?,   date_create = ?,   quantity = ?,   product_id_ref = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.orders_bill_id_ref);
			preState.setDate(2, (java.sql.Date) this.date_create);
			preState.setInt(3, this.quantity);
			preState.setInt(4, this.product_id_ref);
			preState.setInt(5, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.orders_bill_detail where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, orders_bill_id_ref, date_create, quantity, product_id_ref from dbo.orders_bill_detail  where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Orders_bill_details a = new Orders_bill_details();
				a.id = rs.getInt("id");
				a.orders_bill_id_ref = rs.getInt("orders_bill_id_ref");
				a.date_create = rs.getDate("date_create");
				a.quantity = rs.getInt("quantity");
				a.product_id_ref = rs.getInt("product_id_ref");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public ReturnObj GetBy_OrderBillId(int idBill) {
		String sql = "Select * from dbo.orders_bill_detail where orders_bill_id_ref = ? order by date_create desc";
		
		ReturnObj obj = new ReturnObj();
		ArrayList<Orders_bill_details> arr = new ArrayList<Orders_bill_details>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, idBill);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Orders_bill_details a = new Orders_bill_details();
				a.id = rs.getInt("id");
				a.orders_bill_id_ref = rs.getInt("orders_bill_id_ref");
				a.date_create = rs.getDate("date_create");
				a.quantity = rs.getInt("quantity");
				a.product_id_ref = rs.getInt("product_id_ref");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		System.out.println(arr.size());
		return obj;
	}
}
